<!DOCTYPE html>
<html>
<head>
{include file="top.tpl"}
<style>
    .uploadify-queue{}
</style>
</head>
<body class="skin-blue">
<div class="wrapper">
  {include file="bread_crumbs.tpl"}
  <section class="base-setting-wrap">
    <div class="row">
      <div class="col-md-12">
        <div class="box">
        <form class="form-horizontal" id="edit" action="/base/warehouse/doedit" method="get">
          <input type="hidden" name="id" id="id" value="" />
          <div class="box-body">
            <div class="form-group">
              <label for="cnname" class="col-md-2 control-label">仓库名称:<em class="text-star">*</em></label>
              <div class="col-md-9">
                <input type="text" class="form-control" id="warehouse_name" name="warehouse_name" readonly placeholder="" />
              </div>
            </div>
            <div class="form-group">
              <label for="enname" class="col-md-2 control-label">仓库代码:<em class="text-star">*</em></label>
              <div class="col-md-9">
                <input type="text" class="form-control" id="warehouse_code" name="warehouse_code" readonly placeholder="" />
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">所属事业部:<em class="text-star">*</em></label>
              <div class="col-md-9">
                <select class="form-control" id="dept_id" name="dept_id">
                  {$depts}
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">仓库类型:<em class="text-star">*</em></label>
              <div class="col-md-9">
                <select class="form-control" name="type" id="type">
                  <option value="-">请选择</option>
                  <option value="1">自主</option>
                  <option value="2">合约</option>
                  <option value="3">公共</option>
                  <option value="4">虚拟仓</option>
                  <option value="5">平台虚拟仓</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">是否发货仓库:<em class="text-star">*</em></label>
              <div class="col-md-9">
                <select class="form-control" id="auto_send" name="auto_send">
                  <option value="-">请选择</option>
                  <option value="1">是</option>
                  <option value="0">否</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">是否海外仓:<em class="text-star">*</em></label>
              <div class="col-md-9">
                <select class="form-control" id="is_oversea" name="is_oversea">
                  <option value="-">请选择</option>
                  <option value="1">是</option>
                  <option value="0">否</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">特采数据审核:<em class="text-star">*</em></label>
              <div class="col-md-9">
              	<span class="radio">
                	<label><input type="radio" name="special_data_examine" value="1">无需审核</label>
                </span>
                <span class="radio">
                	<label><input type="radio" name="special_data_examine" value="2">一级审核</label>
                </span>
                <span class="radio">
                	<label><input type="radio" name="special_data_examine" value="3">二级审核</label>
                </span>
                <span class="radio">
                  <label><input type="radio" name="special_data_examine" value="4">三级审核</label>
                </span>
                <p class="help-block form-group-tips">备注：选择主管审核后，则需要先组长审核再主管审核，如不设置则默认无需审核</p>
               </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">特采需求审核:<em class="text-star">*</em></label>
              <div class="col-md-9">
	              <span class="radio">
	                	<label><input type="radio" name="special_demand_examine" value="1">无需审核</label>
	               </span>
	               <span class="radio">
	                	<label><input type="radio" name="special_demand_examine" value="2">需要审核</label>
	               </span>
               </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">需求审核:<em class="text-star">*</em></label>
              <div class="col-md-9">
                <span class="radio">
                <label>
                  <input type="radio" name="demand_examine" value="1" id="default_sel">
                  无需审核</label>
                </span>
                <span class="radio">
                <label>
                  <input type="radio" name="demand_examine" value="2" id="js_openexame">
                  需要审核</label>
                </span>
                </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">备注:<em class="text-star">*</em></label>
              <div class="col-md-9">
              	  <textarea class="form-control" name="comment" id="comment" rows="" cols=""></textarea>
              </div>
            </div>
          </div>
          <!-- /.box-body -->
          <div class="box-footer text-center">
            <button type="submit" class="btn btn-primary btn-sm">保存</button>
            <a href="/base/warehouse/" class="btn btn-default btn-sm">返回</a> </div>
          <!-- /.box-footer -->
          <!--批量审核 弹窗-->
          <div class="layer-dialog" id="js_batch_check_dialog">
            <div class="pt15 pl15 pr15">
                <table class="table table-bordered no-margin" id="examine_boxes">
                  <tr>
                      <td width="150" class="text-left">
                        <label>
                          <input name="l_leader[auditor]" class="js_select" type="checkbox" value="1" {if isset($warehouse_ex[1])}checked{/if}>
                          <input type="hidden" name="l_leader[warehouse_code]" class="warehouse_code" />
                          <b>小组长审核：</b>
                        </label>
                      </td>
                      <td class="col-md-9 text-left form-inline">
                          <label>总金额达到 <input type="text" class="form-control" name="l_leader[quota]" placeholder="请输入数字" value="{if isset($warehouse_ex[1]) &&$warehouse_ex.1.quota!=0}{$warehouse_ex.1.quota}{/if}" data-level="小组长"> 元</label>
                          <label>或数量达到 <input type="text" class="form-control" name="l_leader[maxnum]" placeholder="请输入数字" value="{if isset($warehouse_ex[1]) && $warehouse_ex.1.maxnum!=0}{$warehouse_ex.1.maxnum}{/if}" data-level="小组长"> 个</label>
                      </td>
                  </tr>
                  <tr>
                      <td width="150" class="text-left">
                        <label>
                          <input name="b_leader[auditor]" class="js_select" type="checkbox" value="2" {if isset($warehouse_ex[2])}checked{/if}>
                          <input type="hidden" name="b_leader[warehouse_code]" class="warehouse_code" />
                          <b>大组长审核：</b>
                        </label>
                      </td>
                      <td class="col-md-9 text-left form-inline">
                          <label>总金额达到 <input type="text" class="form-control" name="b_leader[quota]" placeholder="请输入数字" value="{if isset($warehouse_ex[2]) &&$warehouse_ex.2.quota!=0}{$warehouse_ex.2.quota}{/if}" data-level="大组长"> 元</label>
                          <label>或数量达到 <input type="text" class="form-control" name="b_leader[maxnum]" placeholder="请输入数字" value="{if isset($warehouse_ex[2]) && $warehouse_ex.2.maxnum!=0}{$warehouse_ex.2.maxnum}{/if}" data-level="大组长"> 个</label>
                      </td>
                  </tr>
                  <tr>
                      <td width="150" class="text-left">
                        <label>
                          <input name="director[auditor]" class="js_select" type="checkbox" value="3" {if isset($warehouse_ex[3])}checked{/if}>
                          <input type="hidden" name="director[warehouse_code]" class="warehouse_code" />
                          <b>主管审核：</b>
                        </label>
                      </td>
                      <td class="col-md-9 text-left form-inline">
                          <label>总金额达到 <input type="text" class="form-control" name="director[quota]" placeholder="请输入数字" value="{if isset($warehouse_ex[3]) && $warehouse_ex.3.quota!=0}{$warehouse_ex.3.quota}{/if}" data-level="主管"> 元</label>
                          <label>或数量达到 <input type="text" class="form-control" name="director[maxnum]" placeholder="请输入数字" value="{if isset($warehouse_ex[3]) && $warehouse_ex.3.maxnum!=0}{$warehouse_ex.3.maxnum}{/if}" data-level="主管"> 个</label>
                      </td>
                  </tr>
                  <tr>
                      <td width="150" class="text-left">
                        <label>
                          <input name="manager[auditor]" class="js_select" type="checkbox" value="4" {if isset($warehouse_ex[4])}checked{/if}>
                          <input type="hidden" class="warehouse_code" name="manager[warehouse_code]" />
                          <b>经理审核：</b>
                        </label>
                      </td>
                      <td class="col-md-9 text-left form-inline">
                          <label>总金额达到 <input type="text" class="form-control" name="manager[quota]" placeholder="请输入数字" value="{if isset($warehouse_ex[4]) && $warehouse_ex.4.quota!=0}{$warehouse_ex.4.quota}{/if}" data-level="经理"> 元</label>
                          <label>或数量达到 <input type="text" class="form-control" name="manager[maxnum]" placeholder="请输入数字" value="{if isset($warehouse_ex[4]) && $warehouse_ex.4.maxnum!=0}{$warehouse_ex.4.maxnum}{/if}" data-level="经理"> 个</label>
                      </td>
                  </tr>
                </table>
            </div>
          </div>
        </form>
        </div>
      </div>
    </div>
  </section>
</div>
{literal}
<style>
.uploadify-queue{}
.img-thumbnail{width:50px;height:50px}
.layer-dialog{display:none}
</style>
{/literal}
<div id="warehouse_data" data=""></div>
<script src="{$js.url}lib/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="{$js.url}lib/bootstrap.min.js" type="text/javascript"></script>
<!-- <script src="{$js.url}stock/stock.js" type="text/javascript"></script> -->
<script src="{$js.url}plugins/uploadify/jquery.uploadify.min.js" type="text/javascript"></script>
<script src="{$js.url}plugins/layer.js" type="text/javascript"></script>
<script src="{$js.url}common.js"></script>
<script>
    var data = {$warehouse};
    $('#warehouse_name').val(data[0]['warehouse_name']);
    $('#warehouse_code').val(data[0]['warehouse_code']);
    $('.warehouse_code').val(data[0]['warehouse_code']);
    $('#comment').val(data[0]['comment']);
    $('#id').val(data[0]['id']);
    setSelected('type');
    setSelected('dept_id');
    setSelected('auto_send');
    setSelected('is_oversea');
    $('input[name="special_data_examine"]').eq(data[0]['special_data_examine'] - 1).prop('checked', 'checked');
    $('input[name="special_demand_examine"]').eq(data[0]['special_demand_examine'] - 1).prop('checked', 'checked');
    $('input[name="demand_examine"]').each(function (){
        if($(this).val() == data[0]['demand_examine']){
            $(this).prop('checked', 'checked');
        }
    });
    // $('input[name="demand_examine"]').eq(data[0]['demand_examine'] - 1).prop('checked', 'checked');
    function setSelected(strID){
        $('#' + strID + ' option').each(function (i){
            if($(this).val() == data[0][strID]){
                $(this).prop('selected', 'selected');
                return false;
            }
        });
    }

    //没选择输入框则为可读状态
    $('#examine_boxes input[type="checkbox"]').each(function(i,v){
        if(!$(v).prop('checked')){
            $(v).parents("tr").find('input[type="text"]').attr('readonly','');
            i = i+2;
            $('#examine_boxes tr:nth-child(n+'+i+')').find('input[type="checkbox"]').prop('disabled',true);
        }
    });

    //勾选小组长审核后，才能勾选大组长审核、主管审核、经理审核
    $('.js_select').on('change',function(){
        var thisTr = $(this).parents("tr"),
            index = thisTr.index() +1;
        if($(this).prop('checked')){
            if(index != 4){
                thisTr.next("tr").find('.js_select').prop('disabled',false);
            }

            thisTr.find('input').removeAttr('readonly');
        }else{

            if(index != 4){
                var index_s = index +1,
                    nextTr = $('#examine_boxes tr:nth-child(n+'+index_s+')');
                nextTr.find('.js_select').prop('disabled',true).prop('checked',false);
                nextTr.find('input[type="text"]').attr('readonly','').val('');
            }

            thisTr.find('input[type="text"]').attr('readonly','').val('');
        }
    });

    //点击了“无需审核”后，再点击“需要审核”后，则输入框全部为空
    $("#default_sel").on('click',function(){
        $('input[name="l_leader[auditor]"]').trigger('click');
    });
</script>
<script>
$('#js_uploadCatImg').uploadify({
    'formData':{
        'timestamp' : '2',
        'token'     : '1'
    },
    'swf' : '../js/plugins/uploadify/uploadify.swf',
    'uploader' : '../js/plugins/uploadify/uploadify.php',
    'fileTypeExts' : '*.jpg; *.png',
    'uploadLimit' : 1,
    'buttonText' : '&nbsp;&nbsp;选择图片', //按钮文字
    'buttonClass':'btn btn-default btn fa fa-plus', //按钮样式
    'removeCompleted' : false,
    'width' : 101, //按钮宽度
    'height' : 34, //按钮高度
    'auto' : false, //自动上传
    'multi' : false,//
    'checkExisting' : true,//检测重复
    //将队列显示框放到指定的容器
    'onSelect' : function(file){
        // console.log(file);
        $("#js_uploadCatImg-queue").appendTo("#js_queenHolder")
    },
    //上传完成回调
    'onUploadSuccess' : function(file, data, response) {
        $("#js_catImgHolder").removeClass("hidden").find("img").attr("src",data.url);
    }
});
$('#js_openexame').click(function (){
  layer.open({
      type:"1",
      title:"需求审核",
      area:["600px","auto"],
      content:$("#js_batch_check_dialog"),
      btn:["确定"],
      yes:function(index){
        /*var index = layer.load(1, {
          shade: [0.1,'#fff'] //0.1透明度的白色背景
        });*/
        // S.submiter($('#examine_select'));
        var checked = checkExamineInit();
        if(checked != false){
            layer.msg(checked);
            return false;
        }
        layer.close(index);
      },
      end:function(){
         if(!$('input[name="l_leader[auditor]"]').prop('checked')){
            $('#default_sel').prop('checked',true);
         }
      }
  });
});
//上传按钮
$("#js_uploadBtn").on("click",function(){
    $('#js_uploadCatImg').uploadify('upload','*');
});

function checkExamineInit(){
  var l_leader_quota = Number($('input[name="l_leader[quota]"]').val());
  var l_leader_maxnum = Number($('input[name="l_leader[maxnum]"]').val());
  var b_leader_quota = Number($('input[name="b_leader[quota]"]').val());
  var b_leader_maxnum = Number($('input[name="b_leader[maxnum]"]').val());
  var director_quota = Number($('input[name="director[quota]"]').val());
  var director_maxnum = Number($('input[name="director[maxnum]"]').val());
  var manager_quota = Number($('input[name="manager[quota]"]').val());
  var manager_maxnum = Number($('input[name="manager[maxnum]"]').val());

  //金额或数量不能全为空
  var isStop = false;
  $('.js_select').each(function(i,v){
    var thisTr = $(v).parents("tr");
    if($(v).prop('checked')){
        if(thisTr.find('input[type="text"]').eq(0).val() == '' && thisTr.find('input[type="text"]').eq(1).val() == ''){
            isStop = true;
        }
    }
  });
  if(isStop){
    return '选定了审核人，请填写金额或数量';
  }

  //输入比0大的数字
  var ipt = $("#examine_boxes").find("input[type='text']"),
      isError = false;
  $(ipt).each(function(i,v){
    if($(v).val() != '' && !/^[1-9][0-9]*$/.test($(v).val())){
        isError = true;
    }
  });
  if(isError){
    return '须填写比0大的整数';
  }

  //级别小审核的金额或数量需比级别大审核的小
  var amount = $("input[name$='[quota]']"),
      val = [];
  $(amount).each(function(i,v){
    var ipt_val = $(v).val(),
        level = $(v).data("level");
    if(ipt_val != ''){
        val.push(ipt_val,level);
    }
  });

  for(var j=0;j<val.length;j++){
    if(Number(val[j]) >= Number(val[j+2])){
        return val[j+1]+'设定的审核金额过大';
        return false;
    }
  }

  var num = $("input[name$='[maxnum]']"),
      val2 = [];
  $(num).each(function(i,v){
    var num_val = $(v).val(),
        level2 = $(v).data("level");
    if(num_val != ''){
        val2.push(num_val,level2);
    }
  });

  for(var j=0;j<val2.length;j++){
    if(Number(val2[j]) >= Number(val2[j+2])){
        return val2[j+1]+'设定的审核数量过大';
        return false;
    }
  }
  return false;
}

$('#edit').submit(function (){
  if($('#comment').val() == ''){
    layer.msg('备注不能为空。');
    return false;
  }
  var checked = checkExamineInit();
  if(checked != false){
    layer.msg(checked);
    return false;
  }
  var index = layer.load(1, {
    shade: [0.1,'#fff'] //0.1透明度的白色背景
  });
  $.ajax({
    type : 'post',
    dataType : 'json',
    url : '/base/warehouse/doedit',
    data : $(this).serialize(),
    success : function (de){
      layer.close(index);
      layer.msg(de.message);
      if(de.status == 0){
        return false;
      }
      setTimeout(function (){
        location.href = '/base/warehouse/index';
      }, 2000);
    }
  });
  return false;
});
</script>
</body>
</html>
